package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mapper.FormFieldMapper;
import com.qs.mapper.FormMapper;
import com.qs.mapper.entity.Form;
import com.qs.mapper.entity.FormField;
import com.qs.service.FormFieldService;
import com.qs.service.FormService;
import com.qs.service.bo.FormBO;
import com.qs.service.bo.FormFieldBO;
import com.qs.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class FormServiceImpl extends ServiceImpl<FormMapper, Form> implements FormService {

    private final FormFieldMapper formFieldMapper;

    @Override
    public String add(FormBO formBO) {
        Form form = BeanCopyUtils.copyBean(formBO, Form.class);
        isExist(Form::getName, form.getName());
        boolean saved = save(form);
        // 添加默认表单列
        FormField formField1 = new FormField();
        formField1.setFormId(form.getId());
        formField1.setName("createUser");
        formField1.setTitle("创建人");
        formField1.setFixed("right");
        formField1.setSort(1);
        formFieldMapper.insert(formField1);
        FormField formField2 = new FormField();
        formField2.setFormId(form.getId());
        formField2.setName("createTime");
        formField2.setTitle("创建时间");
        formField2.setFixed("right");
        formField2.setSort(1);
        formFieldMapper.insert(formField2);
        FormField formField3 = new FormField();
        formField3.setFormId(form.getId());
        formField3.setName("updateUser");
        formField3.setTitle("更新人");
        formField3.setFixed("right");
        formField3.setSort(1);
        formFieldMapper.insert(formField3);
        FormField formField4 = new FormField();
        formField4.setFormId(form.getId());
        formField4.setName("updateTime");
        formField4.setTitle("更新时间");
        formField4.setFixed("right");
        formField4.setSort(1);
        formFieldMapper.insert(formField4);
        return saved ? "新增成功" : "新增失败";
    }

    @Override
    public String edit(FormBO formBO) {
        Form form = BeanCopyUtils.copyBean(formBO, Form.class);
//        isExist(Form::getName, form.getName());
        LambdaQueryWrapper<Form> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Form::getId, form.getId());
        boolean updated = update(form, lambdaQueryWrapper);
        return updated ? "修改成功" : "修改失败";
    }

    @Override
    public String delete(Long id) {
        boolean removed = removeById(id);
        return removed ? "删除成功" : "删除失败";
    }

    @Override
    public FormBO getFormByName(String name) {
        LambdaQueryWrapper<Form> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Form::getName, name);
        Form form = baseMapper.selectOne(lambdaQueryWrapper);
        return BeanCopyUtils.copyBean(form, FormBO.class);
    }

    private void isExist(SFunction<Form, ?> column, Object value) {
        LambdaQueryWrapper<Form> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(column, value);
        Form form = getOne(queryWrapper);
        if (Objects.nonNull(form)) {
            throw new RuntimeException("表单已存在");
        }
    }
}
